import os
import sqlite3

import requests
from lxml import etree

from tbitems_db_vip import initdb, insertdb


def parsehtml(cursor,strhtml):
    print(cursor)
    html=etree.HTML(strhtml)
    zuozhe = html.xpath('//*[@id="moderate"]/table/tbody/tr/td[2]/cite/text()')
    namelist =[]
    for i in zuozhe:
        if i !='\r\n':
            namelist.append(i)
    titles=html.xpath('//*[@id="moderate"]/table/tbody/tr/th/a')
    print(titles)
    num =0
    for title in titles:
        webtitle = title.text  #存标题
        if '版块主题' in webtitle:
            continue
        webzuozhe =namelist[num]
        webhref = title.get('href')  #存链接
        webhref =f'http://bbs.8080.net/{webhref}'
        webcontent = geturl(webhref)
        webstatus = 0
        print(webzuozhe)
        print(webtitle)
        print(webhref)
        insertdb(cursor,webtitle,webhref,webstatus,'','','','','','',webzuozhe,webcontent)
        num = num + 1
        # insertdb(cursor, '123', '123', '213', '', '', '', '', '', '')

        conn.commit()

    # # session.close()
    #多线程运行

def geturl(url):
    # header={}
    response=requests.get(url=url)
    # print('请求拿到的信息编码格式为 %s' %response.encoding)
    # print('请求拿到的信息编码格式为 %s' %response.apparent_encoding)
    if response.encoding == 'ISO-8859-1':
        a=response.text.encode(response.encoding).decode(response.apparent_encoding)
        print('a====%s' %a)
        print(type(a))
        return a
    else:
        # print(str(response.text))
        # print(response.content.decode('utf-8', 'ignore'))
        return response.text

def geturls(cursor):
    for i in range(744,1000):
        url = f'http://bbs.8080.net/forum-160-{i}.html'
        print(url)
        text1 = geturl(url)
        parsehtml(cursor, text1)
from concurrent import futures
# def ThreadLine(cursor):
#     print('213')
#     with futures.ThreadPoolExecutor(max_workers=1) as executor:  # 多线程
#         #for循环进行多线程调用
#         for i in range(500):
#             #在循环内实例化，在外边实例化会让所有的值一致
#             executor.submit(geturls,i,cursor)

if __name__ == '__main__':
    dbpath = os.path.join('JiSu.db')
    print(dbpath)
    conn = sqlite3.connect(dbpath)
    cursor = conn.cursor()
    initdb(cursor)
    geturls(cursor)
    cursor.close()